﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using System.ComponentModel;
using Telerik.WinControls.UI;
using System.Globalization;
using Telerik.WinControls;
using Telerik.Charting;
using System.IO;
using Control_Searcher;
using BE;
using Telerik.WinControls.RichTextBox;
using Telerik.WinControls.UI.Docking;


namespace SearchControls
{

    public class ReadInfo
    {
        private int newTag
        {
            get
            {
                if (TagTexts.Count == 0)
                    return 1;
                return TagTexts.Keys.Last() + 1;
            }

        }
        public List<Form> forms;
        public List<StringBuilder> RunTimeFileChange; //saving a version of the form file in memory before changing it so texts can be found by the algorithm
        public Dictionary<string, ControlInfo> texts;  // texts indexed after their text value, this dictionary contains all new texts found both in updates and when a new form is searched
        public SortedDictionary<int, ControlInfo> TagTexts; //texts indexed by the tag they have
        public Dictionary<string, ControlInfo> nameTexts;//controls to be tagged in the designer.cs class, indexed by it's NameSpace.FormName.VariableName
        public Dictionary<string, ControlInfo> oldTexts; //when updating old texts that are already in the database are index here
        public Dictionary<string, ControlInfo> oldNames;//contains master variable names from texts already in the db
        public Dictionary<int, ControlInfo> tagChanges; //tags with a text update
        public Dictionary<int, ControlInfo> TagTransfers; //when updating texts and a control is given a text that already exists it needs its tag changed nothing more. It checks if the tag it has should be changed to something else
        public List<string> files; // where the .cs files are located for the forms
        public CultureInfo CurrentCulture;
        public Dictionary<int, Dictionary<int, ControlInfo>> askable;  // for lists and other controls with items that should be asked if the user wants to save them in the database
        public Dictionary<string, ControlInfo> NameTagChange; //A control that got a new text and a new tag, it needs it's tag changed in the designer class, when inserting it checks on the variable name if it needs it's tag changed
        private Form currentForm;
        public Dictionary<int, List<ControlInfo>> NameSlaves;//if a control is a slave it's saved here so that if a new master is needed they can be found in this index
        public Dictionary<string, List<ControlInfo>> RunTimeTexts; //indexing runtime texts so they can be found when saving them to the file, 
        public List<int> NewTagMasters; //tags that need a new master control, if a master deletes it's tag and is given a new text or is given a new text that already exists the old tag might need a new master if it is found in a newly added tag
        public Dictionary<string, bool> SaveList; //dictionary af askables der husker om det skal gemmes eller ikke gemmes
        //forskellen på tagtransferes og nametag change
        //nametagchange er hvis en enkelt komponent skal have et nyt tag
        //tagtransfer er hvis alle komponenter med det tag skal have skiftet deres tag
        public ReadInfo(CultureInfo c)
            : this()
        {
            CurrentCulture = c;//new CultureInfo("en-GB")

        }

        public void FindRunTimeTexts(string path)
        {
           
            StreamReader reader = new StreamReader(path);
            string line = string.Empty;
            
            while ((line = reader.ReadLine()) != null)
            {
                string[] fs = currentForm.GetType().ToString().Split('.');
                string FormClassName = fs[fs.Length-1];
                //hvis constructoren er fundet stoppes søgningen da alle tekster ligger mellem den og klasse deklerationen
                if ((line.Contains("public ") || line.Contains("internal ") || line.Contains("private ") || line.Contains("protected ")) && line.Contains(FormClassName + "("))
                {
                    break;
                }
                if (line.Contains("string ") && (line.Contains("public ") || line.Contains("internal ") || line.Contains("private ") || line.Contains("protected "))) //string er fundet
                {
                    
                    int stringIdx = line.IndexOf("string ");
                    if (!(line.Contains("//") && line.IndexOf("//") < stringIdx)) //hvis string deklarationen er efter et // er den udkommenteret
                    {
                        //finder tekstværdiener
                        int startIdx = line.IndexOf("\"")+1;
                        int endIdx = line.IndexOf("\";");
                        string text = line.Substring(startIdx, endIdx-startIdx);
                        
                        //finder variabelnavnet
                        int varStartIdx = line.IndexOf("string ",StringComparison.OrdinalIgnoreCase)+7; //ignorer store/små bogstaver da string både kan starte med stort og lille
                        int varEndIdx = line.IndexOf("=");
                        string varName = line.Substring(varStartIdx, varEndIdx - varStartIdx).Replace(" ","");
                        
                        if (line.Contains("// #%TAG = "))
                        {//finder tagget hvis det eksisterer
                            int idx = line.IndexOf("// #%TAG = ")+11;
                            int tag = Convert.ToInt32(line.Substring(idx));
                            addRunTimeText(varName,text,tag); 
                        }
                        else
                        addRunTimeText(varName, text);
                    }
                    
                }
                
            }
            reader.Close();


        }

        

        public ReadInfo()
        {
            if (CurrentCulture == null)
                CurrentCulture = new CultureInfo("en-GB");
            RunTimeFileChange = new List<StringBuilder>();
            forms = new List<Form>();
            texts = new Dictionary<string, ControlInfo>();
            oldTexts = new Dictionary<string, ControlInfo>();
            TagTransfers = new Dictionary<int, ControlInfo>();
            askable = new Dictionary<int, Dictionary<int, ControlInfo>>();
            nameTexts = new Dictionary<string, ControlInfo>();
            TagTexts = new SortedDictionary<int, ControlInfo>();
            tagChanges = new Dictionary<int, ControlInfo>();
            NameSlaves = new Dictionary<int, List<ControlInfo>>();
            NameTagChange = new Dictionary<string, ControlInfo>();
            NewTagMasters = new List<int>();
            oldNames = new Dictionary<string, ControlInfo>();
            RunTimeTexts = new Dictionary<string, List<ControlInfo>>();
            prepareUpdate();
        }
        public void prepareUpdate()
        {//forbereder opdaterings procedure ved at hente information om nuværende tekster og eksisterende lister og om de skal behandles eller ej
            List<Language> LanguageList = languageConnector.getLanguagesByDefaultLanguage();
            foreach (Language lan in LanguageList)
            {

                ControlInfo current = new ControlInfo();
                current.FullName = lan.MasterControl;
                current.Tag = lan.TagId;
                current.Text = lan.Text;
                TagTexts.Add(current.Tag, current);
                oldTexts.Add(current.Text, current);
                if (!lan.MasterControl.Equals(""))
                    oldNames.Add(current.FullName, current);
                else
                    NewTagMasters.Add(current.Tag);
            }
            SaveList = languageConnector.getSaveList();
        }
        public void saveAllTexts()
        {
            int cf = 0;
            foreach (Form f in forms)
            {
                currentForm = f;
                ControlInfo inf = addText(f);
                inf.isForm = true;
                extractTexts(f);

                if (f.MainMenuStrip != null)
                {
                    MenuStrip strip = f.MainMenuStrip;
                    findMenuItems(strip.Items);

                }
                FindRunTimeTexts(files[cf]);
                if (f is IManualWorks)
                {
                    IManualWorks e = (IManualWorks)f;
                    List<ControlInfo> list = e.GetUserDefinedControlTexts();
                    foreach (ControlInfo ci in list)
                    {

                        addMasterText(ci);
                    }
                }
                cf++;

            }
        }

        private void findMenuItems(ToolStripItemCollection toolStripItemCollection)
        {


            //windows toolstrips
            foreach (ToolStripItem item in toolStripItemCollection)
            {
                addText(item);
                if (item is ToolStripComboBox)//toolstripcombobox and should not enter recursive
                {
                    ToolStripComboBox v = (ToolStripComboBox)item;
                    if (v.Items.Count > 0)
                    {
                        //in case toolstripcombobox gets another list of items, logic for adding texts is placed here
                    }
                }
                else if (item is ToolStripDropDownItem) //dropdown item and should enter recursive
                {
                    ToolStripDropDownItem v = (ToolStripDropDownItem)item;
                    if (v.DropDownItems.Count > 0)
                    {
                        findMenuItems(v.DropDownItems);
                    }
                }

            }
        }
        public static List<Control> GetControls(Control form)
        {
            var controlList = new List<Control>();

            foreach (Control childControl in form.Controls)
            {
                // Recurse child controls.
                controlList.AddRange(GetControls(childControl));
                controlList.Add(childControl);
            }
            return controlList;
        }
        #region addTextMethods
        // disse addtext metoder gør en controlinfo klar ud fra det komponent den modtager, sætter formnavn, komponentnavn, tekst, tag samt andre ting i Controlinfo objektet
        //list og index sættes i addaskabletext og addtextwithoutname metoderne
        private ControlInfo addText(ToolStripItem item)
        {
            ControlInfo currentInf = new ControlInfo();
            currentInf.Name = item.Name;
            if (item.Tag != null && item.Tag is int)
            {
                currentInf.Tag = (int)item.Tag;
            }
            currentInf.Text = item.Text;
            currentInf.FormName = currentForm.GetType().ToString();

            return addMasterText(currentInf);
        }

        private ControlInfo addRunTimeText(string varName, string text)
        {
            ControlInfo currentInf = new ControlInfo();
            currentInf.Name = varName;
            currentInf.Text = text;
            currentInf.FormName = currentForm.GetType().ToString();
            appendRunTimeTextToList(currentInf);
            return addMasterText(currentInf);
        }

        private void appendRunTimeTextToList(ControlInfo currentInf)
        {
            if (!RunTimeTexts.ContainsKey(currentForm.GetType().ToString()))
                RunTimeTexts.Add(currentForm.GetType().ToString(), new List<ControlInfo>());
            RunTimeTexts[currentForm.GetType().ToString()].Add(currentInf);
            currentInf.simpleTagged = false;
        }
        private ControlInfo addRunTimeText(string varName, string text,int tag)
        {
            ControlInfo currentInf = new ControlInfo();
            currentInf.Name = varName;
            currentInf.Tag=tag;
            currentInf.Text = text;
            currentInf.FormName = currentForm.GetType().ToString();
            appendRunTimeTextToList(currentInf);
            return addMasterText(currentInf);
        }
        private ControlInfo addText(ColumnHeader item, string parent, int listNumber) //simpletagged = true
        {
            ControlInfo currentInf = new ControlInfo();
           
            currentInf.Name = item.Name;
            currentInf.parentControl = parent;
            currentInf.ParentType = "Microsoft ListViewControl";
            currentInf.Text = item.Text;
            currentInf.FormName = currentForm.GetType().ToString();
            if (item.Tag != null && item.Tag is int)
            {
                currentInf.Tag = (int)item.Tag;
            }

            currentInf.parentControl = parent;
            return addAskableText(currentInf, parent, listNumber);
        }
        private ControlInfo addText(ListViewGroup item, string parent, int listNumber) //simpletagged=true
        {
            ControlInfo currentInf = new ControlInfo();
            currentInf.Name = item.Name;
            currentInf.parentControl = parent;
            currentInf.ParentType = "Microsoft ListViewControl";
            currentInf.Text = item.Header;
            currentInf.FormName = currentForm.GetType().ToString();
            if (item.Tag != null && item.Tag is int)
            {
                currentInf.Tag = (int)item.Tag;
            }

            currentInf.parentControl = parent;
            return addAskableText(currentInf, parent, listNumber);
        }
        private void addTextToCollection(ControlInfo addThis)
        {
            if (addThis.Text.Length > 0 && !texts.ContainsKey(addThis.Text) && !oldTexts.ContainsKey(addThis.Text))
            {
                
                addThis.Tag = newTag;
                TagTexts.Add(addThis.Tag, addThis);
                texts.Add(addThis.Text, addThis);
            }
        }
        /* private ControlInfo addText(OpenFileDialog ofd)
         {
             ControlInfo currentInf = new ControlInfo();
             currentInf.Name = "";
             if(ofd.Tag is int)
             currentInf.Tag = (int)ofd.Tag;
             currentInf.Text = ofd.Title;
             currentInf.FormName = currentForm.GetType().ToString();
             return addMasterText(currentInf);
         }*/


        private ControlInfo addText(Control ctr)
        {

            Object o = (Object)ctr;
            ControlInfo currentInf = new ControlInfo(ctr.Tag, o, currentForm.GetType().ToString());
            if (currentInf.Name.Length < 1)
                return currentInf;
            return addMasterText(currentInf);
        }
        private ControlInfo addText(RadChart ctr)
        {/*
            
            ControlInfo currentInf = new ControlInfo();
            currentInf.Name = ctr.Name;
            currentInf.Text = ctr.ChartTitle.TextBlock.Text;
            currentInf.FormName = currentForm.GetType().ToString();
            if (ctr.Tag != null && ctr.Tag is int)
                currentInf.Tag = (int)ctr.Tag;
            return addMasterText(currentInf);*/
            return null;
        }
        private ControlInfo addText(ChartSeriesItem ctr)
        {
            /*
            ControlInfo currentInf = new ControlInfo();
            currentInf.Name = ctr.Name;
            currentInf.Text = ctr.Label.TextBlock.Text;
            currentInf.FormName = currentForm.GetType().ToString();
            if (texts.ContainsKey(currentInf.Text))
                currentInf.Tag = texts[currentInf.Text].Tag;
            else if (oldTexts.ContainsKey(currentInf.Text))
                currentInf.Tag = oldTexts[currentInf.Text].Tag;
            
            return addMasterText(currentInf);*/
            return null;
        }
        private ControlInfo addText(ChartSeries ctr)
        {
            /* ControlInfo currentInf = new ControlInfo();
             currentInf.Name = ctr.Name;
             currentInf.Text = ctr.Name;
             currentInf.FormName = currentForm.GetType().ToString();
             if (texts.ContainsKey(currentInf.Text))
                 currentInf.Tag = texts[currentInf.Text].Tag;
             else if (oldTexts.ContainsKey(currentInf.Text))
                 currentInf.Tag = oldTexts[currentInf.Text].Tag;

             return addMasterText(currentInf);*/
            return null;
        }
        private ControlInfo addText(GridViewColumn col, string parent)
        {
            ControlInfo currentInf = new ControlInfo();
            if (col.Name.Contains('$'))
            {
                string[] inf = col.Name.Split('$');
                currentInf.Name = inf[0];
                currentInf.Tag = Convert.ToInt32(inf[1]);
            }
            else
            {
                currentInf.Name = col.Name;
            }
            currentInf.Text = col.HeaderText;
            currentInf.parentControl = parent;
            currentInf.FormName = currentForm.GetType().ToString();
            currentInf.simpleTagged = false;
            return addMasterText(currentInf);

        }
        private ControlInfo addText(DataGridViewColumn col, string parent)
        {
            /*ControlInfo currentInf = new ControlInfo();
            if (col.Name.Contains('$'))
            {
                string[] inf = col.Name.Split('$');
                currentInf.Name = inf[0];
                currentInf.Tag = Convert.ToInt32(inf[1]);
            }
            else
            {
                currentInf.Name = col.Name;
            }
            
            currentInf.Text = col.HeaderText;
            currentInf.parentControl = parent;
            currentInf.FormName = currentForm.GetType().ToString();
            currentInf.simpleTagged = false;
            return addMasterText(currentInf);*/
            return null;
        }
        private ControlInfo addText(RadElement element)
        {
            object o = (Object)element;
            ControlInfo currentInf = new ControlInfo(element.Tag, o, currentForm.ToString());
            return addMasterText(currentInf);
        }
        //create a new addmastertext with another object type to use it as a master, note that the master MUST have the name property which states it's variable name

        private ControlInfo addText(TreeNode element, string parent, int list)
        {
            ControlInfo currentInf = new ControlInfo();
            currentInf.Name = element.Name;
            currentInf.parentControl = parent;
            currentInf.ParentType = "Microsoft Treeview";
            currentInf.Text = element.Text;
            currentInf.FormName = currentForm.GetType().ToString();
            if (element.Tag != null && element.Tag is int)
            {
                currentInf.Tag = (int)element.Tag;
            }

            currentInf.parentControl = parent;
            currentInf.simpleTagged = false;
            return addAskableText(currentInf, parent, list);
        }
        private ControlInfo addText(RadTreeNode element, string parent, int list)
        {
            ControlInfo currentInf = new ControlInfo();
            currentInf.Name = element.Name;
            currentInf.parentControl = parent;
            currentInf.ParentType = "Telerik TreeViewControl";
            currentInf.Text = element.Text;
            currentInf.FormName = currentForm.GetType().ToString();
            if (element.Tag != null && element.Tag is int)
            {
                currentInf.Tag = (int)element.Tag;
            }

            currentInf.parentControl = parent;
            currentInf.simpleTagged = false;
            return addAskableText(currentInf, parent, list);
        }
        private ControlInfo addText(ListViewItem item, string parent, int listNumber, string listName) //skal tilføjes som radlistdataitem
        {
            ControlInfo currentInf = new ControlInfo();
            currentInf.Name = listName;
            currentInf.parentControl = parent;
            currentInf.ParentType = "Microsoft ListView";
            currentInf.Text = item.Text;
            currentInf.FormName = currentForm.GetType().ToString();
            if (item.Tag != null && item.Tag is int)
            {
                currentInf.Tag = (int)item.Tag;
            }

            currentInf.parentControl = parent;
            currentInf.simpleTagged = false;
            return addTextWithoutName(currentInf, parent, listNumber);
        }
        private ControlInfo addText(ListViewDataItem element, string parent, int list, string listName)
        {

            ControlInfo currentInf = new ControlInfo();
            currentInf.Name = listName;
            currentInf.parentControl = parent;
            currentInf.ParentType = "Telerik ListView";
            currentInf.Text = element.Text;
            currentInf.FormName = currentForm.GetType().ToString();
            if (element.Tag != null && element.Tag is int)
            {
                currentInf.Tag = (int)element.Tag;
            }

            currentInf.parentControl = parent;
            currentInf.simpleTagged = false;
            return addTextWithoutName(currentInf, parent, list);
        }
        private ControlInfo addText(RadListDataItem item, string parent, int list, string listName, string type)
        {
            object o = (Object)item;
            ControlInfo currentInf = new ControlInfo();
            currentInf.Text = item.Text;
            currentInf.FormName = currentForm.GetType().ToString();
            currentInf.parentControl = parent;
            currentInf.Name = listName;
            currentInf.ParentType = type;
            currentInf.simpleTagged = false;
            if (item.Tag != null && item.Tag is int)
                currentInf.Tag = (int)item.Tag;

            return addTextWithoutName(currentInf, parent, list);
        }
        //tilføjer en ny tekst som har master prioritet over komponenter i askablelister, dette er så at hvis tilfældet opstår at en tekst i en askable liste og en tekst der er i eksempelvis et label har samme vædi
        //denne metode vil overtage master statusen og fjerne dens entry i askablelisten så selvom listen ikke skal gemmes vil teksten blive gemt alligevel
        //den vil kun overtage tekster der opstår i samme tekstfindings session.
        private ControlInfo addMasterText(ControlInfo currentInf)
        {
            if (currentInf.Text.Equals(""))
                return null;
            if (currentInf.Tag > 0 && !TagTexts[currentInf.Tag].Text.Equals(currentInf.Text)) //if control has a tag and the text doesn't match the text bound to the tag
            {

                if (oldNames.ContainsKey(currentInf.FullName)) //master found need an update to this tag
                {//hvis teksten findes i databasen vil tagget markeres som at dets tag skiftet til det tag der tilhører teksten
                    if (oldTexts.ContainsKey(currentInf.Text))
                    {
                        TagTransfers.Add(currentInf.Tag, oldTexts[currentInf.Text]);
                        NewTagMasters.Add(currentInf.Tag);
                    }//hvis teksten er tilføjet i denne session vil tagget markeres som at dets tag skiftet til det tag der tilhører teksten
                    else if (texts.ContainsKey(currentInf.Text))
                    {
                        TagTransfers.Add(currentInf.Tag, texts[currentInf.Text]);
                        NewTagMasters.Add(currentInf.Tag);
                    }
                    else
                    { //hvis teksten ikke eksisteres skal teksten i databasen ændres og tagget markeret som ændret så eventuelle oversættelser kan blive rettet til at fitte
                        tagChanges.Add(currentInf.Tag, currentInf);
                        currentInf.AddToDb = false;
                        texts.Add(currentInf.Text, currentInf);
                    }
                    //add to texts with a boolean that states it should not be added to the database or should it
                }
                else //slave got new text that needs to be saved as well as a new text with its own tag
                {
                    if (oldTexts.ContainsKey(currentInf.Text))
                        currentInf.Tag = oldTexts[currentInf.Text].Tag;
                    else if (texts.ContainsKey(currentInf.Text)) //text is in new texts
                        currentInf.Tag = texts[currentInf.Text].Tag;
                    else
                        addTextToCollection(currentInf);
                    NameTagChange.Add(currentInf.FullName, currentInf); // saved here so that it can get a new tag id assigned.
                }
            }
            else if (currentInf.Tag < 1 && oldTexts.ContainsKey(currentInf.Text) && !oldNames.ContainsKey(currentInf.FullName)) //new control but with text that already exist
            {
                currentInf.Tag = oldTexts[currentInf.Text].Tag;
                nameTexts.Add(currentInf.FullName, currentInf);
                if (!NameSlaves.ContainsKey(currentInf.Tag))
                {
                    NameSlaves.Add(currentInf.Tag, new List<ControlInfo>());
                }
                NameSlaves[currentInf.Tag].Add(currentInf);
            }

            else if (currentInf.Tag < 1)
            {
                int current = texts.Count;
                addTextToCollection(currentInf);
                if (current < texts.Count) //if text was added
                {

                }
                else//if text was not added
                {
                    if (oldTexts.ContainsKey(currentInf.Text))
                    {

                    }//giv den det tag der eksisterer til teksten
                    if (texts.ContainsKey(currentInf.Text))
                        currentInf.Tag = texts[currentInf.Text].Tag;
                    else if (oldTexts.ContainsKey(currentInf.Text))
                        currentInf.Tag = oldTexts[currentInf.Text].Tag;
                    if (!nameTexts.ContainsKey(currentInf.FullName))
                        nameTexts.Add(currentInf.FullName, currentInf);
                    if (TagTexts[currentInf.Tag].list > 0)//hvis denne tekst findes i en askable overtager den master rollen og entryet i askable listen fjernes samt selve listen hvis den nu er tom
                    {
                        askable[TagTexts[currentInf.Tag].list].Remove(TagTexts[currentInf.Tag].index);
                        if (askable[TagTexts[currentInf.Tag].list].Count < 1)
                            askable.Remove(askable[TagTexts[currentInf.Tag].list].First().Value.list);
                        texts[currentInf.Text] = currentInf;
                        TagTexts[currentInf.Tag] = currentInf;
                    }
                    else
                    {//hvis ikke det er en askable er det nu en slave og vil blive tilføjet nameslaves dictionarien i tilfælde af at teksten skal have ny master er denne controlinfo nu kandidat til titlen
                        if (!NameSlaves.ContainsKey(currentInf.Tag))
                        {
                            NameSlaves.Add(currentInf.Tag, new List<ControlInfo>());
                        }
                        NameSlaves[currentInf.Tag].Add(currentInf);
                    }
                }
                if (oldNames.ContainsKey(currentInf.FullName))//control is master but has no tag
                {
                    NewTagMasters.Add(oldNames[currentInf.FullName].Tag);
                    if (!nameTexts.ContainsKey(currentInf.FullName))
                        nameTexts.Add(currentInf.FullName, currentInf); //tilføjes nametexts igen, selvom den er master har den intet tag
                    //derfor skal det have sat dets nye tag i designer filen og tilføjes derfor til nametexts
                }
                else //completely new control
                {
                    if (!nameTexts.ContainsKey(currentInf.FullName))
                        nameTexts.Add(currentInf.FullName, currentInf);
                }
            }

            return currentInf;
        }
        //tilføj en tekst som skal ligge i en askable liste
        private ControlInfo addAskableText(ControlInfo currentInf, string parent, int List)//the new index is just found by looking at the count and always adding that number as the index
        {

            //if text is not added it should not be appended in the list of askables
            if (currentInf.Text.Equals(""))
                return null;
            if (currentInf.Tag > 0 && !TagTexts[currentInf.Tag].Text.Equals(currentInf.Text)) //if control has a tag and the text doesn't match the text bound to the tag
            {
                if (oldNames.ContainsKey(currentInf.FullName)) //master found need an update to this tag
                {
                    if (oldTexts.ContainsKey(currentInf.Text))
                    {
                        TagTransfers.Add(currentInf.Tag, oldTexts[currentInf.Text]);
                        NewTagMasters.Add(currentInf.Tag);
                    }
                    else if (texts.ContainsKey(currentInf.Text))
                    {
                        TagTransfers.Add(currentInf.Tag, texts[currentInf.Text]);
                        NewTagMasters.Add(currentInf.Tag);
                    }
                    else
                    {
                        tagChanges.Add(currentInf.Tag, currentInf);
                        currentInf.AddToDb = false;
                        texts.Add(currentInf.Text, currentInf);
                    }
                    //add to texts with a boolean that states it should not be added to the database or should it
                }
                else //slave got new text that needs to be saved as well as a new text with its own tag
                {
                    if (oldTexts.ContainsKey(currentInf.Text))
                        currentInf.Tag = oldTexts[currentInf.Text].Tag;
                    else if (texts.ContainsKey(currentInf.Text)) //text is in new texts
                        currentInf.Tag = texts[currentInf.Text].Tag;
                    else
                        addTextToCollection(currentInf);
                    NameTagChange.Add(currentInf.FullName, currentInf); // saved here so that it can get a new tag id assigned.
                }
            }
            else if (currentInf.Tag < 1 && oldTexts.ContainsKey(currentInf.Text) && !oldNames.ContainsKey(currentInf.FullName)) //new control but with text that already exist
            {
                currentInf.Tag = oldTexts[currentInf.Text].Tag;
                nameTexts.Add(currentInf.FullName, currentInf);
                if (!NameSlaves.ContainsKey(currentInf.Tag))
                {
                    NameSlaves.Add(currentInf.Tag, new List<ControlInfo>());
                }
                NameSlaves[currentInf.Tag].Add(currentInf);
            }

            else if (currentInf.Tag < 1)
            {
                int current = texts.Count;
                addTextToCollection(currentInf);
                if (current < texts.Count) //if text was added
                {
                    currentInf.list = List;
                    currentInf.index = askable[currentInf.list].Count;
                    askable[currentInf.list].Add(currentInf.index, currentInf);


                }
                else//if text was not added
                {
                    if (texts.ContainsKey(currentInf.Text))
                        currentInf.Tag = texts[currentInf.Text].Tag;
                    else if (oldTexts.ContainsKey(currentInf.Text))
                        currentInf.Tag = oldTexts[currentInf.Text].Tag;
                    nameTexts.Add(currentInf.FullName, currentInf);

                    if (!NameSlaves.ContainsKey(currentInf.Tag))
                    {
                        NameSlaves.Add(currentInf.Tag, new List<ControlInfo>());
                    }
                    NameSlaves[currentInf.Tag].Add(currentInf);

                }
                if (oldNames.ContainsKey(currentInf.FullName))//control is master but has no tag
                {
                    NewTagMasters.Add(oldNames[currentInf.FullName].Tag);
                    if (!nameTexts.ContainsKey(currentInf.FullName))
                        nameTexts.Add(currentInf.FullName, currentInf);
                }
                else //completely new control
                {
                    if (!nameTexts.ContainsKey(currentInf.FullName))
                        nameTexts.Add(currentInf.FullName, currentInf);
                }
            }

            return currentInf;
        }
       //tilføj en tekst som ikke har en name property i komponenten (teleriks listbox)
        private ControlInfo addTextWithoutName(ControlInfo currentInf, string parent, int list)
        {

            if (currentInf.Text.Equals(""))
                return null;
            //UNIQUE FOR LISTDATAITEM
            ControlInfo MasterInfo=null;
            if(TagTexts.ContainsKey(currentInf.Tag))
             MasterInfo = TagTexts[currentInf.Tag];
            string matcher = currentForm.GetType().ToString() + "." + parent;
            //to check if it's a master that has a new text (like the first iff's in the other addTexts, 
            if (currentInf.Tag > 0 && !TagTexts[currentInf.Tag].Text.Equals(currentInf.Text)) //if control has a tag and the text doesn't match the text bound to the tag
            {

                if (MasterInfo.FullName.Contains(matcher)) //master found need an update to this tag
                {
                    if (oldTexts.ContainsKey(currentInf.Text))
                    {
                        TagTransfers.Add(currentInf.Tag, oldTexts[currentInf.Text]);
                        NewTagMasters.Add(currentInf.Tag);
                    }
                    else if (texts.ContainsKey(currentInf.Text))
                    {
                        TagTransfers.Add(currentInf.Tag, texts[currentInf.Text]);
                        NewTagMasters.Add(currentInf.Tag);
                    }
                    else
                    {
                        tagChanges.Add(currentInf.Tag, currentInf);
                        currentInf.AddToDb = false;
                        texts.Add(currentInf.Text, currentInf);
                    }

                }
                else //slave got new text that needs to be saved as well as a new text with its own tag 
                {
                    if (oldTexts.ContainsKey(currentInf.Text))
                        currentInf.Tag = oldTexts[currentInf.Text].Tag;
                    else if (texts.ContainsKey(currentInf.Text)) //text is in new texts
                        currentInf.Tag = texts[currentInf.Text].Tag;
                    else
                        addTextToCollection(currentInf);
                    NameTagChange.Add(currentInf.FullName, currentInf);
                }
            }
            else if (currentInf.Tag < 1 && oldTexts.ContainsKey(currentInf.Text) && !oldNames.ContainsKey(currentInf.FullName)) //new control but with text that already exist
            {
                currentInf.Tag = oldTexts[currentInf.Text].Tag;
                if (!NameSlaves.ContainsKey(currentInf.Tag))
                {
                    NameSlaves.Add(currentInf.Tag, new List<ControlInfo>());
                }
                NameSlaves[currentInf.Tag].Add(currentInf);
            }

            else if (currentInf.Tag < 1)
            {
                int current = texts.Count;
                addTextToCollection(currentInf);
                if (current < texts.Count) //if text was added
                {
                    currentInf.list = list;
                    currentInf.index = askable[currentInf.list].Count;
                    askable[currentInf.list].Add(currentInf.index, currentInf);
                    if (!nameTexts.ContainsKey(currentInf.FullName))
                        nameTexts.Add(currentInf.FullName, currentInf);
                }
                else//if text was not added
                {
                    if (texts.ContainsKey(currentInf.Text))
                        currentInf.Tag = texts[currentInf.Text].Tag;
                    else if (oldTexts.ContainsKey(currentInf.Text))
                        currentInf.Tag = oldTexts[currentInf.Text].Tag;

                    if (!NameSlaves.ContainsKey(currentInf.Tag))
                    {
                        NameSlaves.Add(currentInf.Tag, new List<ControlInfo>());
                    }
                    NameSlaves[currentInf.Tag].Add(currentInf);

                }
                if (MasterInfo != null && MasterInfo.FullName.Contains(matcher))//control is master but has no tag
                {
                    NewTagMasters.Add(oldNames[currentInf.FullName].Tag);

                }
                else //completely new control
                {
                    if (!nameTexts.ContainsKey(currentInf.FullName))
                        nameTexts.Add(currentInf.FullName, currentInf);
                }
            }
            return currentInf;
        }
        #endregion
        private void extractTexts(Control c)
        {
            foreach (Control cs in GetControls(c))
            {
                ControlInfo cInfo;
                if (isNotInvalidControl(c))
                {
                    cInfo = addText(cs);
                }
                else
                {
                    cs.ToString(); //created fr debugging purposes
                }

                if (cs.Controls.Count > 0)
                {
                    
                    foreach (Control co in cs.Controls)
                    {
                        //adding text from new control
                        extractTexts(co);
                    }
                    
                }
                

                if (cs is RadMenu)//checker om komponentet har en bestemt type så den kan få bestemt behandling for at finde teksten og sætte tagget
                {
                    

                    RadMenu menuc = ((RadMenu)cs);
                    if (menuc.Items.Count > 0)
                        extractItemsFromCollection(menuc.Items.ToArray<RadElement>());
                }
                else if (cs is RadStatusStrip)
                {
                    extractItemsFromCollection(((RadStatusStrip)cs).Items.ToArray<RadElement>());
                }

                else if (cs is RadGridView)
                {
                    
                    foreach (GridViewColumn col in ((RadGridView)cs).Columns)
                    {
                        
                        addText(col, cs.Name);
                    }
                }
                else if (cs is RadMultiColumnComboBox)
                {
                    foreach (GridViewColumn col in ((RadMultiColumnComboBox)cs).EditorControl.Columns)
                    {
                        
                        addText(col, cs.Name);
                    }
                }
                else if (cs is RadCarousel)
                {
                    RadCarousel car = (RadCarousel)cs;
                    extractItemsFromCollection(car.Items.ToArray<RadElement>());
                }
                else if (cs is RadWizard)
                {//TEMPORARY DISABLED DUE TO HAVING 2 TEXTS BUT ONLY 1 TAG ELEMENT, POSSIBLE SOLUTION A POINT VARIABLE SINCE IT HOLDS 2 INTEGERS
                    /* 
                     RadWizard w = (RadWizard)cs;
                     foreach (WizardPage p in w.Pages)
                     {
                         
                         addText(p.Header);
                         addText(p.Title);
                         extractTexts(p.ContentArea);
                     }//*/
                }
                else if (cs is RadApplicationMenu)
                {
                    extractItemsFromCollection(((RadApplicationMenu)cs).ButtonItems.ToArray<RadElement>());
                    extractItemsFromCollection(((RadApplicationMenu)cs).RightColumnItems.ToArray<RadElement>());
                    extractItemsFromCollection(((RadApplicationMenu)cs).Items.ToArray<RadElement>());
                }
                else if (cs is RadDropDownButton)
                {
                    extractItemsFromCollection(((RadDropDownButton)cs).Items.ToArray<RadElement>());
                }

                else if (cs is RadChart)
                {/* følg definitionen til addText metoderne for implementering af radCharts
                  * Når den skal oversættes skal alle Variabler der bruges lægges i klassen så designer klassen har adgang til dem
                    //de skal også være initialized (var x = new var();) før initializecomponent klassen da processen sætter et tag på variablen
                    //i designer klassen og dette kan kun gøres så længe den er initializeret, Husk at sætte Name propertien på variablerne til navnet på variablen
                  * EX:
                  * ExampleSerie.Name = "ExampleSerie";
                  * fuldt eksempel, udsnit af en form klasse
                  * ...
                  * private ChartSeries ExampleSerie; //Deklarer variable
                  * public ExampleForm() { //Constructor
                  * ExampleSerie = new ChartSeries(); // instantiser den
                  * ExampleSerie.Name = "ExampleSerie", //sæt navn for identifikation
                  * ...(resten af koden der bygger RadChart objektet)...
                  * InitializeComponents();
                  * ...
                    */
                    RadChart chart = (RadChart)cs;

                    addText(chart);
                    foreach (ChartSeries serie in chart.Series)
                    {

                        addText(serie);
                        if (serie.Items.Count > 0)
                        {
                            foreach (ChartSeriesItem citem in serie.Items)
                            {

                                addText(citem);
                            }
                        }
                    }
                }
                else if (cs is RadDropDownList)
                {
                    addFromComboItems(((RadDropDownList)cs).Items, cs.Name, "Telerik DropdownList");
                }
                else if (cs is RadListControl)
                {
                    RadListControl list = (RadListControl)cs;
                    addFromComboItems(list.Items, list.Name, "Telerik ListControl");
                }
                else if (cs is RadPanorama)
                {
                    RadPanorama pano = (RadPanorama)cs;
                    extractItemsFromCollection(pano.Groups.ToArray<RadElement>());
                    extractItemsFromCollection(pano.Items.ToArray<RadElement>());
                }
                else if (cs is RadTreeView)
                {
                    cs.ToString();
                    //ControlInfo info = addText(cs);
                    extractNodes(((RadTreeView)cs).Nodes, cs.Name);
                }
                else if (cs is RadCommandBar)
                {
                    RadCommandBar bar = (RadCommandBar)cs;
                    foreach (CommandBarRowElement row in bar.Rows)
                    {
                        addText(row);
                        foreach (CommandBarStripElement strip in row.Strips)
                        {

                            addText(strip);
                            extractItemsFromCollection(strip.Items.ToArray<RadElement>());
                        }
                    }
                }
                else if (cs is RadRibbonBar)
                {
                    RadRibbonBar ribbon = (RadRibbonBar)cs;
                    extractItemsFromCollection(ribbon.StartMenuItems.ToArray<RadElement>());
                    extractItemsFromCollection(ribbon.StartMenuRightColumnItems.ToArray<RadElement>());
                    extractItemsFromCollection(ribbon.StartMenuBottomStrip.ToArray<RadElement>());
                    extractItemsFromCollection(ribbon.QuickAccessToolBarItems.ToArray<RadElement>());
                    foreach (ContextualTabGroup group in ribbon.ContextualTabGroups)
                    {

                        addText(group);
                        foreach (RibbonTab tab in group.TabItems)
                        {

                            addText(tab);
                            extractItemsFromCollection(tab.Items.ToArray<RadElement>());
                        }
                    }
                }
                else if (cs is RadRotator)
                {
                    RadRotator rotator = (RadRotator)cs;
                    extractItemsFromCollection(rotator.Items.ToArray<RadElement>());
                }
                else if (cs is RadListView)
                {
                    RadListView listView = (RadListView)cs;



                    if (listView.Items.Count > 0)
                    {
                        string parent = listView.Name + "List";
                        if ((!SaveList.ContainsKey(parent)) || (SaveList.ContainsKey(parent) && SaveList[parent]))
                        {
                            int listNumber = askable.Count;
                            Dictionary<int, ControlInfo> viewList = new Dictionary<int, ControlInfo>();
                            askable.Add(listNumber, viewList);
                            int nr = 0;
                            foreach (ListViewDataItem item in listView.Items)
                            {

                                string lName = "ListViewDataItem" + nr;
                                nr++;
                                addText(item, parent, listNumber, lName);
                            }
                            if (SaveList.ContainsKey(parent) && SaveList[parent])
                                removeIndexes(listNumber);
                        }
                    }
                    if (listView.Groups.Count > 0)
                    {
                        string parent = listView.Name + "Group";
                        if ((!SaveList.ContainsKey(parent)) || (SaveList.ContainsKey(parent) && SaveList[parent]))
                        {
                            int listNumber = askable.Count;
                            Dictionary<int, ControlInfo> viewList = new Dictionary<int, ControlInfo>();
                            askable.Add(listNumber, viewList);
                            int nr = 0;
                            foreach (ListViewDataItemGroup item in listView.Groups)
                            {
                                string lName = "ListViewDataItemGroup" + nr;
                                nr++;
                                addText((ListViewDataItem)item, parent, listNumber, lName);
                            }
                            if (SaveList.ContainsKey(parent) && SaveList[parent])
                                removeIndexes(listNumber);
                        }
                    }
                }
                #region win controls

                //#################################Windows controls#########################
                else if (cs is TreeView)
                {
                    addTreeNodes(((TreeView)cs).Nodes, cs.Name);
                }
                else if (cs is DataGridView)
                {
                    DataGridView grid = (DataGridView)cs;
                    foreach (DataGridViewColumn col in grid.Columns)
                    {
                        addText(col, grid.Name);
                    }
                }
                else if (cs is ToolStrip)
                {
                    findMenuItems(((ToolStrip)cs).Items);
                }
                /*
                else if (cs is ListBox)
                {
                    ListBox list = (ListBox)cs;
                    List<String> items = new List<string>();
                    foreach (Object o in list.Items)
                    {
                        int current = texts.Count;
                        addText(o.ToString());
                        if (current < texts.Count)
                            items.Add(o.ToString());
                    }
                    askable.Add(items);
                }*/
                else if (cs is ListView)
                {
                    ListView listView = (ListView)cs;

                    if (listView.Items.Count > 0)
                    {
                        string parent = listView.Name + "ListItems";
                        if ((!SaveList.ContainsKey(parent)) || (SaveList.ContainsKey(parent) && SaveList[parent]))
                        {
                            int listNumber = askable.Count;
                            Dictionary<int, ControlInfo> viewList = new Dictionary<int, ControlInfo>();
                            askable.Add(listNumber, viewList);
                            int nr = 0;
                            foreach (ListViewItem item in listView.Items)
                            {
                                string lName = "ListViewItem" + nr;
                                nr++;
                                addText(item, parent, listNumber, lName);
                                /* if (item.SubItems.Count > 0) //hvis microsoft laver deres listviewsubitems om så de kommer med i designeren
                                 {
                                      string parent = listView.Name+"SubListItems";
                                     int list = askable.Count;
                                     Dictionary<int, ControlInfo> viewSubList = new Dictionary<int, ControlInfo>();
                                     askable.Add(list, viewSubList);
                                 foreach (ListViewItem.ListViewSubItem sub in item.SubItems)
                                 {
                                     addText(sub,parent,list);
                                 }*/
                            }
                            if (SaveList.ContainsKey(parent) && SaveList[parent])
                                removeIndexes(listNumber);
                        }

                    }
                    if (listView.Groups.Count > 0)
                    {
                        string parent = listView.Name + "Groups";
                        if ((!SaveList.ContainsKey(parent)) || (SaveList.ContainsKey(parent) && SaveList[parent]))
                        {
                            int listNumber = askable.Count;
                            Dictionary<int, ControlInfo> viewList = new Dictionary<int, ControlInfo>();
                            askable.Add(listNumber, viewList);
                            foreach (ListViewGroup item in listView.Groups)
                            {
                                addText(item, parent, listNumber);
                            }
                            if (SaveList.ContainsKey(parent) && SaveList[parent])
                                removeIndexes(listNumber);
                        }
                    }
                    if (listView.Columns.Count > 0)
                    {
                        string parent = listView.Name + "Columns";
                        if ((!SaveList.ContainsKey(parent)) || (SaveList.ContainsKey(parent) && SaveList[parent]))
                        {
                            int listNumber = askable.Count;
                            Dictionary<int, ControlInfo> viewList = new Dictionary<int, ControlInfo>();
                            askable.Add(listNumber, viewList);
                            foreach (ColumnHeader item in listView.Columns)
                            {
                                addText(item, parent, listNumber);
                            }
                            if (SaveList.ContainsKey(parent) && SaveList[parent])
                                removeIndexes(listNumber);
                        }
                    }



                }
                #endregion
            }

        }








        private void removeIndexes(int list)
        {
            Dictionary<int, ControlInfo> AskableList = askable[list];
            foreach (ControlInfo ci in AskableList.Values)
            {
                ci.index = 0;
                ci.list = 0;
            }
            askable.Remove(list);
        }

        //invalide kontroller er dato komponenter, kompoenter hvor .text værdien ikke vises og komponenter hvor .text kan redigeres.
        private bool isNotInvalidControl(Control c) //til controller hvis tekst ikke skal gemmes
        {
            return !(c is RadSchedulerNavigator || c is RadScheduler || c is RadColorBox || c is RadPropertyGrid || c is RadBrowseEditor || c is RadSpellChecker || c is RadMaskedEditBox || c is RadTextBox || c is RadScrollablePanel || c is RadPageView || c is RadPanorama || c is RadSplitContainer || c is RadDock || c is RadTrackBar || c is RadWaitingBar || c is RadHScrollBar || c is RadVScrollBar || c is RadRotator || c is RadBreadCrumb || c is RadSeparator || c is RadCarousel || c is RadClock || c is RadRichTextBox || c is RadChart || c is RadGridView || c is RadListView || c is RadListControl || c is TreeView || c is ToolStrip || c is StatusStrip || c is NumericUpDown || c is MenuStrip || c is MaskedTextBox || c is TextBox || c is BindingNavigator || c is RichTextBox || c is RadTreeView || c is RadSpinEditor || c is DateTimePicker || c is MonthCalendar || c is RadTimePickerElement || c is RadTimePicker || c is RadDateTimeEditor || c is RadDateTimePicker || c is RadDateTimeEditorElement || c is RadDateTimePickerCalendar);
        }
        private Boolean isInvalidItem(RadItem im) //når man looper igennem en itemcollection vil denne blive kaldt før tekst tilføjelse for dem der ikke skal tilføjes
        {
            return !(im is RadColorBoxElement || im is RadBrowseEditorElement || im is RadMaskedEditBoxElement || im is RadScrollablePanelElement || im is RadPageViewElement || im is RadPanoramaElement || im is RadTrackBarElement || im is RadTextBoxElement || im is RadWaitingBarElement || im is RadRotatorElement);
        }
        //windows treeview
        private void addTreeNodes(TreeNodeCollection nodes, string parent)
        {
            if ((!SaveList.ContainsKey(parent)) || (SaveList.ContainsKey(parent) && SaveList[parent]))
            {
                int listNumber = askable.Count;
                Dictionary<int, ControlInfo> nodeList;
                if (nodes.Count > 0)
                {
                    nodeList = new Dictionary<int, ControlInfo>();
                    askable.Add(listNumber, nodeList);
                }
                extractNodeLoop(nodes, parent, listNumber);
                if (SaveList.ContainsKey(parent) && SaveList[parent])
                    removeIndexes(listNumber);
            }
        }
        private void extractNodeLoop(TreeNodeCollection nodes, string parent, int list)
        {
            foreach (TreeNode node in nodes)
            {
                if (node.Nodes.Count > 0)
                    extractNodeLoop(node.Nodes, parent, list);
                addText(node, parent, list);
            }
        }

        //Telerik Treeview
        private void extractNodes(RadTreeNodeCollection nodes, string parent)
        {
            if ((!SaveList.ContainsKey(parent)) || (SaveList.ContainsKey(parent) && SaveList[parent]))
            {
                int listNumber = askable.Count;
                Dictionary<int, ControlInfo> nodeList;
                if (nodes.Count > 0)
                {
                    nodeList = new Dictionary<int, ControlInfo>();
                    askable.Add(listNumber, nodeList);
                }
                extractNodeLoop(nodes, parent, listNumber);
                if (SaveList.ContainsKey(parent) && SaveList[parent])
                    removeIndexes(listNumber);
            }
        }
        private void extractNodeLoop(RadTreeNodeCollection nodes, string parent, int list)
        {
            foreach (RadTreeNode node in nodes)
            {
                if (node.Nodes.Count > 0)
                    extractNodeLoop(node.Nodes, parent, list);
                addText(node, parent, list);
            }
        }


        //tilføj items fra en liste af elementer, disse er ikke udsprunget fra Control klassen
        private void extractItemsFromCollection(RadElement[] colle)
        {
            foreach (RadItem im in colle)
            {
                if (isInvalidItem(im))
                    addText(im);
                if (hasItems(im))
                {
                    
                    extractItemsFromCollection(im.Children.ToArray<RadElement>());
                }
            }
        }

        public void removeTexts(List<int> remove)
        {
            List<string> removeList = new List<string>();
            foreach (int idx in remove)
            {
                foreach (ControlInfo text in askable[idx].Values)
                {
                    texts.Remove(text.Text);
                    nameTexts.Remove(text.FullName);
                }
                removeList.Add(askable[idx].First().Value.parentControl);
                askable.Remove(idx);
            }
            languageConnector.RememberLists(removeList, askable);
        }
        //check om raditemet har items i dens kollektion, nogle extendede klasser af 
        //raditem indeholder items på en anden måde og skal derfor behandles anderledes
        public bool hasItems(RadItem item)
        {
           
            if (item is RadItemsContainer)
                return item.Children.Count > 0;
            else if (item is RadRotatorElement)
            {
                extractItemsFromCollection(((RadRotatorElement)item).Items.ToArray<RadElement>());
                return false;
            }
            else if (item is RadMenuComboItem)
            {
                addFromComboItems(((RadMenuComboItem)item).ComboBoxElement.Items, ((RadMenuComboItem)item).Name, "telerik menuDropDownList");
                return false;
            }
            else if (item is RadDropDownListElement)
            {
                addFromComboItems(((RadDropDownListElement)item).Items, ((RadDropDownListElement)item).Name, "Telerik Dropdownlist Element");
                return false;
            }
            else if (item is CommandBarDropDownButton)
            {
                extractItemsFromCollection(((CommandBarDropDownButton)item).Items.ToArray<RadElement>());
                return false;
            }
            else if (item is CommandBarDropDownList)
            {
                addFromComboItems(((CommandBarDropDownList)item).Items, ((CommandBarDropDownList)item).Name, "Telerik Commandbar Dropdownlist");
                return false;
            }
            else if (item is RadMenuButtonItem)
            {
                RadMenuButtonItem but = (RadMenuButtonItem)item;
                extractItemsFromCollection(but.Items.ToArray<RadElement>());
                return false;
            }
            else if (item is RadMenuItemBase)
            {
                extractItemsFromCollection(((RadMenuItemBase)item).Items.ToArray<RadElement>());
                return false;
            }
            else if (item is TileGroupElement)
            {
                extractItemsFromCollection(((TileGroupElement)item).Items.ToArray<RadElement>());
                return false;
            }



            return false;
        }

        private void addFromComboItems(RadListDataItemCollection items, string parent, string parentType)
        {//tilføj items fra en kollektion af radlistdataitems, forbered dem på askable liste indsættelse
            if ((!SaveList.ContainsKey(parent)) || (SaveList.ContainsKey(parent) && SaveList[parent]))
            {
                int listNumber = askable.Count;
                Dictionary<int, ControlInfo> itemList;
                if (items.Count > 0)
                {
                    itemList = new Dictionary<int, ControlInfo>();
                    askable.Add(listNumber, itemList);
                }
                int number = 0;
                foreach (RadListDataItem item in items)
                {
                    addText(item, parent, listNumber, "RadListItem" + number, parentType);
                    number++;
                }
                if (SaveList.ContainsKey(parent) && SaveList[parent])
                    removeIndexes(listNumber);
            }


        }


        public void SaveToDb()
        {
            languageConnector.SendListToDb(texts,CurrentCulture); //gem tekster
            languageConnector.ChangeTextOnTags(tagChanges, CurrentCulture); //skift tekst på tags
            languageConnector.TransferMaster(NewTagMasters, NameSlaves); //skift master kontrollen hvis der findes andre som bruger teksten
            saveTagsToForm();
        }
        //gem tags til .designer.cs filen
        private void saveTagsToForm()
        {
            
            string ManagerVariable = Operations.getConfiguration().AppSettings.Settings[Operations.CONF_MANAGER_OPTION].Value;
            int formidx = 0;
            foreach (string path in files)
            {
                bool b = true;
                string line = string.Empty;
                List<string> fileBuilder = new List<string>();
                string[] fs = forms[formidx].GetType().ToString().Split('.');
                string FormClassName = fs[fs.Length - 1];
                StreamReader reader;

                //gem runtime tekster hvis der er nogen
                if (RunTimeTexts.Count > 0)
                { 
                reader = new StreamReader(path);
               
                while ((line = reader.ReadLine()) != null)
                {
                  
                    foreach (ControlInfo ci in RunTimeTexts[forms[formidx].GetType().ToString()])
                    {
                        //se om linjen indeholder string teksten og variabelnavnet
                        if (line.Contains(ci.Text) && line.Contains(ci.Name))
                        {
                            
                          ///hvis den indeholder dette betyder det at den allerede har et tag
                            if (line.Contains("// #%TAG = "))
                            {
                                int idx = line.IndexOf("// #%TAG = ") + 11;
                                int tag = Convert.ToInt32(line.Substring(idx));
                                //se om tagget skal skiftes i tagtransfers eller nametagchange dictionarierne
                                if (NameTagChange.ContainsKey(ci.FullName))
                                    line = line.Replace("// #%TAG = " + tag, "// #%TAG = " + NameTagChange[ci.FullName].Tag);
                                else if (TagTransfers.ContainsKey(tag))
                                    line = line.Replace("// #%TAG = " + tag, "// #%TAG = " + TagTransfers[tag].Tag);
                            }
                            else
                                line = line+" // #%TAG = "+ci.Tag;
                            
                        }
                    }
                    //hvis den indeholder denne linje går vi ud af loopet og starter på en ny med noget andet funktionalitet
                    if (line.Contains("public ") && line.Contains(FormClassName + "("))
                    {
                        fileBuilder.Add(line);
                        break;
                    }
         
                        fileBuilder.Add(line);
                    
                }
                while ((line = reader.ReadLine()) != null)
                {
                    //hvis linjen indeholder dette checkes tagget og variabelnavnet om det skal have dets tag skiftet baseret på tagchange
                    if(line.Contains("/*#%Er behandlet@oversættelsesScript(må ikke ændres)%#*/"))
                    {
                        int NameStartIdx = line.IndexOf(".getText(") + 9;
                        int NameEndIdx = line.IndexOf(',', NameStartIdx);
                        int TagStartIdx = NameEndIdx + 1;
                        int TagEndIdx = line.IndexOf(')', TagStartIdx);
                        string varName = line.Substring(NameStartIdx, NameEndIdx-NameStartIdx);
                        int tag = Convert.ToInt32(line.Substring(TagStartIdx,TagEndIdx-TagStartIdx));

                        if (TagTransfers.ContainsKey(tag))
                            line = line.Replace("," + tag + ")", "," + TagTransfers[tag].Tag + ")");
                        else if (NameTagChange.ContainsKey(varName))
                            line = line.Replace("," + tag + ")", "," + NameTagChange[varName].Tag + ")");
                    }
                    else//erstat variablen med et kald til manager klassen så den kan finde teksten på det nuværende sprog
                    foreach (ControlInfo ci in RunTimeTexts[forms[formidx].GetType().ToString()])
                    { 
                        if(line.Contains(ci.Name))
                        { line = line.Replace(ci.Name, ManagerVariable + ".getText("+ci.Name+","+ci.Tag+")" + "/*#%Er behandlet@oversættelsesScript(må ikke ændres)%#*/"); } //erstat string variablen med textkald
                    }
                    fileBuilder.Add(line);
                }
               
                reader.Close();
                File.WriteAllLines(path, fileBuilder.ToArray());
                 }
                //adding to the designer class
                string newPath = path.Replace(".cs", ".Designer.cs");

                List<string> builder = new List<string>();
                reader = new StreamReader(newPath);
                
                b = true;
                while (b && (line = reader.ReadLine()) != null)
                {//tilføj linjer fra designer.cs filen til vi er inde i initializecomponent metoden
                    builder.Add(line);
                    if (line.Contains("InitializeComponent()"))
                        b = false;

                }
                b = true;
                while (b && (line = reader.ReadLine()) != null)
                {
                    if (line.Contains("System.Windows.Forms.ListViewItem listViewItem"))
                    {//listview items og listviewgroups behandles i denne del
                        builder.Add(line);
                        string text;
                        string variableName;
                        string startidx = " = new System.Windows.Forms.ListViewItem(\"";
                        int txtstart = line.IndexOf(startidx) + startidx.Length;
                        int txtEnd = line.IndexOf("\");");
                        text = line.Substring(txtstart, txtEnd - txtstart);
                        text = text.Replace("\\", "");

                        int startIdx = line.IndexOf("listViewItem");
                        int endIdx = line.IndexOf(" = new System.Windows.Forms.ListViewGroup(");
                        variableName = line.Substring(startIdx, endIdx - startIdx);
                        if (texts.ContainsKey(text))
                        {
                            //hvis den næste linje indeholder .tag får den det tag den skal have ellers tilføjes tagget
                            string line2 = reader.ReadLine();
                            if (line2.Contains(".Tag"))
                            {
                                //if you need to see the previous tag of a control do it when a new tag is found and save the former tag as going on deletion if it's a master.

                                builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                            }
                            else
                            {
                                builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                                builder.Add(line2);
                            }
                        }
                        else
                        {
                            string line2 = reader.ReadLine();
                            if (line2.Contains(".Tag"))
                            {

                                int tagstart = line.IndexOf(".Tag = ") + 7;
                                int tagEnd = line.IndexOf(";");
                                int nr = Convert.ToInt32(line.Substring(tagstart, tagEnd - tagstart));
                                if (TagTransfers.ContainsKey(nr))
                                {
                                    builder.Add("            " + variableName + ".Tag = " + TagTransfers[nr].Tag + ";");
                                }
                                else
                                {
                                    builder.Add(line2);
                                }
                                if (!TagTexts[nr].Text.Equals(text))
                                {
                                    builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                                }
                            }
                            else
                            {
                                builder.Add(line2);
                            }

                        }
                    }
                    if (line.Contains(".Tag ="))
                    {
                        int startIdx = line.IndexOf("this.") + 5;
                        int endIdx = line.IndexOf(".Tag");
                        if (endIdx < startIdx && endIdx > 0) //if end index is lower than start index it means that it found the tag of the form
                        {
                            string key = forms[formidx].GetType().ToString() + "." + forms[formidx].Text;
                            if (NameTagChange.ContainsKey(key))
                            {
                                builder.Add("this.Tag=" + NameTagChange[key].Tag + ";");
                            }
                        }
                        else
                        {
                            string variableName = line.Substring(startIdx, endIdx -startIdx );
                            if (NameTagChange.ContainsKey(variableName))
                            {

                                line = "this." + NameTagChange[variableName].Name + ".Tag = " + NameTagChange[variableName].Tag + ";";
                            }



                            int tagstart = line.IndexOf(".Tag = ") + 7;
                            int tagEnd = line.IndexOf(";");
                            int nr = Convert.ToInt32(line.Substring(tagstart, tagEnd - tagstart));
                            if (TagTransfers.ContainsKey(nr))
                            {
                                builder.Add("            " + variableName + ".Tag = " + TagTransfers[nr].Tag + ";");
                            }
                        }

                    }
                    else if (line.Contains("            //")) //hvis linjen indeholder dette betyder det at designer klassen er nået til den del hvor forskellige komponenter får tildelt værdier i dens properties
                    {//her bliver taggene indsat (når designvinduet åbnes igen bliver disse flyttet)
                        foreach (ControlInfo info in nameTexts.Values)
                        {
                            if (info.FormName.Equals(forms[formidx].GetType().ToString()) && info.simpleTagged)//her tilføjes værdier fra nametexts dictionarien hvis de har det rigtige formname og har simpletagged som true
                            {
                                if (info.isForm)
                                    builder.Add("this.Tag=" + info.Tag + ";");
                                else
                                    builder.Add("this." + info.Name + ".Tag = " + info.Tag + ";");
                            }
                        }

                        b = false;
                    }

                    builder.Add(line);

                }

                while ((line = reader.ReadLine()) != null)
                {//check om det er en bestem form for komponent hvis variabelnavn != komponent.Name
                    if (line.Contains("listViewDataItemGroup") && line.Contains(".Text ="))
                    {
                        builder.Add(line);
                        string text;
                        string variableName;
                        int txtstart = line.IndexOf("= \"") + 3;
                        int txtEnd = line.IndexOf("\";");
                        text = line.Substring(txtstart, txtEnd - txtstart);
                        text = text.Replace("\\", "");

                        int startIdx = line.IndexOf("listViewDataItemGroup");
                        int endIdx = line.IndexOf(".Text");
                        variableName = line.Substring(startIdx, endIdx - startIdx);
                        if (texts.ContainsKey(text))
                        {

                            string line2 = reader.ReadLine();
                            if (line2.Contains(".Tag"))
                            {
                                //if you need to see the previous tag of a control do it when a new tag is found and save the former tag as going on deletion if it's a master.

                                builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                            }
                            else
                            {
                                builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                                builder.Add(line2);
                            }
                        }
                        else
                        {
                            string line2 = reader.ReadLine();
                            if (line2.Contains(".Tag"))
                            {

                                int tagstart = line.IndexOf(".Tag = ") + 7;
                                int tagEnd = line.IndexOf(";");
                                int nr = Convert.ToInt32(line.Substring(tagstart, tagEnd - tagstart));
                                if (TagTransfers.ContainsKey(nr))
                                {
                                    builder.Add("            " + variableName + ".Tag = " + TagTransfers[nr].Tag + ";");
                                }
                                else
                                {
                                    builder.Add(line2);
                                }
                                if (!TagTexts[nr].Text.Equals(text))
                                {
                                    builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                                }
                            }
                            else
                            {
                                builder.Add(line2);
                            }

                        }
                    }
                    else if (line.Contains("listViewDataItem") && line.Contains(".Text ="))
                    {
                        builder.Add(line);
                        string text;
                        string variableName;
                        int txtstart = line.IndexOf("= \"") + 3;
                        int txtEnd = line.IndexOf("\";");
                        text = line.Substring(txtstart, txtEnd - txtstart);
                        text = text.Replace("\\", "");

                        int startIdx = line.IndexOf("listViewDataItem");
                        int endIdx = line.IndexOf(".Text");
                        variableName = line.Substring(startIdx, endIdx - startIdx);
                        if (texts.ContainsKey(text))
                        {

                            string line2 = reader.ReadLine();
                            if (line2.Contains(".Tag"))
                            {
                                //if you need to see the previous tag of a control do it when a new tag is found and save the former tag as going on deletion if it's a master.

                                builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                            }
                            else
                            {
                                builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                                builder.Add(line2);
                            }
                        }
                        else
                        {
                            string line2 = reader.ReadLine();
                            if (line2.Contains(".Tag"))
                            {

                                int tagstart = line.IndexOf(".Tag = ") + 7;
                                int tagEnd = line.IndexOf(";");
                                int nr = Convert.ToInt32(line.Substring(tagstart, tagEnd - tagstart));
                                if (TagTransfers.ContainsKey(nr))
                                {
                                    builder.Add("            " + variableName + ".Tag = " + TagTransfers[nr].Tag + ";");
                                }
                                else
                                {
                                    builder.Add(line2);
                                }
                                if (!TagTexts[nr].Text.Equals(text))
                                {
                                    builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                                }
                            }
                            else
                            {
                                builder.Add(line2);
                            }

                        }
                    }
                    else if (line.Contains(".Text =") && (line.Contains("radListDataItem") || line.Contains("descriptionTextListDataItem")))
                    {

                        builder.Add(line);
                        string text;
                        string variableName;
                        int txtstart = line.IndexOf("= \"") + 3;
                        int txtEnd = line.IndexOf("\";");
                        text = line.Substring(txtstart, txtEnd - txtstart);
                        text = text.Replace("\\", "");

                        int startIdx;
                        if (line.Contains("descriptionTextListDataItem"))
                        { startIdx = line.IndexOf("descriptionTextListDataItem"); }
                        else
                        { startIdx = line.IndexOf("radListDataItem"); }
                        int endIdx = line.IndexOf(".Text");
                        variableName = line.Substring(startIdx, endIdx - startIdx);
                        if (texts.ContainsKey(text))
                        {

                            string line2 = reader.ReadLine();
                            if (line2.Contains(".Tag"))
                            {
                                //if you need to see the previous tag of a control do it when a new tag is found and save the former tag as going on deletion if it's a master.

                                builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                            }
                            else
                            {
                                builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                                builder.Add(line2);
                            }
                        }
                        else
                        {
                            string line2 = reader.ReadLine();
                            if (line2.Contains(".Tag"))
                            {

                                int tagstart = line.IndexOf(".Tag = ") + 7;
                                int tagEnd = line.IndexOf(";");
                                int nr = Convert.ToInt32(line.Substring(tagstart, tagEnd - tagstart));
                                if (TagTransfers.ContainsKey(nr))
                                {
                                    builder.Add("            " + variableName + ".Tag = " + TagTransfers[nr].Tag + ";");
                                }
                                else
                                {
                                    builder.Add(line2);
                                }
                                if (!TagTexts[nr].Text.Equals(text))
                                {
                                    builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                                }
                            }
                            else
                            {
                                builder.Add(line2);
                            }

                        }

                    }
                    else if (line.Contains("radTreeNode") && line.Contains(".Text ="))
                    {
                        builder.Add(line);
                        string text;
                        string variableName;
                        int txtstart = line.IndexOf("= \"") + 3;
                        int txtEnd = line.IndexOf("\";");
                        text = line.Substring(txtstart, txtEnd - txtstart);
                        text = text.Replace("\\", "");
                        int startIdx = line.IndexOf("radTreeNode");
                        int endIdx = line.IndexOf(".Text =");
                        variableName = line.Substring(startIdx, endIdx - startIdx);
                        if (texts.ContainsKey(text))
                        {



                            string line2 = reader.ReadLine();
                            if (line2.Contains(".Tag"))
                            {
                                //if you need to see the previous tag of a control do it when a new tag is found and save the former tag as going on deletion if it's a master.

                                builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                            }
                            else
                            {
                                builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                                builder.Add(line2);
                            }
                        }
                        else
                        {
                            string line2 = reader.ReadLine();
                            if (line2.Contains(".Tag"))
                            {

                                int tagstart = line.IndexOf(".Tag = ") + 7;
                                int tagEnd = line.IndexOf(";");
                                int nr = Convert.ToInt32(line.Substring(tagstart, tagEnd - tagstart));
                                if (TagTransfers.ContainsKey(nr))
                                {
                                    builder.Add("            " + variableName + ".Tag = " + TagTransfers[nr].Tag + ";");
                                }
                                else
                                {
                                    builder.Add(line2);
                                }
                            }
                            else
                            {
                                builder.Add(line2);
                            }

                        }
                    }
                    else if (line.Contains("radTreeNode") && line.Contains(".Text ="))
                    {
                        builder.Add(line);
                        string text;
                        string variableName;
                        int txtstart = line.IndexOf("= \"") + 3;
                        int txtEnd = line.IndexOf("\";");
                        text = line.Substring(txtstart, txtEnd - txtstart);
                        text = text.Replace("\\", "");
                        int startIdx = line.IndexOf("treeNode");
                        int endIdx = line.IndexOf(".Text =");
                        variableName = line.Substring(startIdx, endIdx - startIdx);
                        if (texts.ContainsKey(text))
                        {



                            string line2 = reader.ReadLine();
                            if (line2.Contains(".Tag"))
                            {
                                //if you need to see the previous tag of a control do it when a new tag is found and save the former tag as going on deletion if it's a master.

                                builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                            }
                            else
                            {
                                builder.Add("            " + variableName + ".Tag = " + texts[text].Tag + ";");
                                builder.Add(line2);
                            }
                        }
                        else
                        {
                            string line2 = reader.ReadLine();
                            if (line2.Contains(".Tag"))
                            {

                                int tagstart = line.IndexOf(".Tag = ") + 7;
                                int tagEnd = line.IndexOf(";");
                                int nr = Convert.ToInt32(line.Substring(tagstart, tagEnd - tagstart));
                                if (TagTransfers.ContainsKey(nr))
                                {
                                    builder.Add("            " + variableName + ".Tag = " + TagTransfers[nr].Tag + ";");
                                }
                                else
                                {
                                    builder.Add(line2);
                                }
                            }
                            else
                            {
                                builder.Add(line2);
                            }

                        }
                    }
                    else if (line.Contains(".HeaderText =") && line.Contains("GridView") && line.Contains("Column"))//needs to go in when it finds headertext and after that loop until it finds .Name so it can find the tag associated with the text it holds I have to do this since I at this point don't know the name of the parent and can't find it by going into the names dictionary as column names can be the same if they are in different gridviews,  so I have to loop through the lines till I get the name variable and can change it.
                    {//if the text then are in new texts it means it got a new text, if not then the tag should be changed because the text already exists, and the previous tag is the one taht should be deleted in the database.
                        string text;
                        string variableName;
                        int txtstart = line.IndexOf("= \"") + 3;
                        int txtEnd = line.IndexOf("\";");
                        text = line.Substring(txtstart, txtEnd - txtstart);
                        text = text.Replace("\\", "");
                        List<string> builder2 = new List<string>();
                        string myLine;
                        while (true)
                        {
                            myLine = reader.ReadLine();
                            if (myLine.Contains(".Name ="))
                                break;
                            builder2.Add(myLine);
                        }
                        int Namestart = line.IndexOf("= \"") + 3;
                        int NameEnd = line.IndexOf("\";");
                        string ColName = myLine.Substring(Namestart, NameEnd - Namestart);
                        if (texts.ContainsKey(text))
                        {
                            //int startIdx;
                            //startIdx = line.IndexOf("gridView");

                            // int endIdx = line.IndexOf(".HeaderText");
                            //variableName = line.Substring(startIdx, endIdx - startIdx);

                            if (myLine.Contains("$"))
                            {
                                /*int txtstart = line.IndexOf('=')+1;
                               string text = line.Substring(txtstart).Replace(" ", "");*/
                                //if need the previous tag just see the if sentence where a control that has a tag gets a new text.
                                string[] cn = ColName.Split('$');
                                string newname = cn[0] + "$" + texts[text].Tag;
                                myLine.Replace(ColName, newname);
                            }
                            else
                            {
                                string newname = ColName + "$" + texts[text].Tag;
                                myLine.Replace(ColName, newname);
                            }
                        }
                        else
                        {
                            if (myLine.Contains('$'))
                            {
                                string[] cn = ColName.Split('$');
                                int colTag = Convert.ToInt32(cn[1]);
                                if (TagTransfers.ContainsKey(colTag))
                                {
                                    string newname = cn[0] + "$" + TagTransfers[colTag].Tag;
                                    myLine.Replace(ColName, newname);
                                }
                            }
                        }



                        builder.Add(line);
                        if (builder2.Count > 0)
                            builder.AddRange(builder2);
                        builder.Add(myLine);
                    }
                    //check om linjen indeholder .Tag = hvis den gør det skal den checkes for opdateringer til dette tag 
                    else if (line.Contains(".Tag ="))
                    {
                        int startIdx = line.IndexOf("this.") + 5;
                        int endIdx = line.IndexOf(".Tag");
                        if (endIdx < startIdx && endIdx > 0) //if end index is lower than start index it means that it found the tag of the form
                        {
                            string key = forms[formidx].GetType().ToString() + "." + forms[formidx].Text;
                            if (NameTagChange.ContainsKey(key))
                            {
                                builder.Add("this.Tag=" + NameTagChange[key].Tag + ";");
                            }
                        }
                        else
                        {
                            string variableName = line.Substring(startIdx, endIdx-startIdx);
                            if (NameTagChange.ContainsKey(variableName))
                            {

                                line = "this." + NameTagChange[variableName].Name + ".Tag = " + NameTagChange[variableName].Tag + ";";
                            }



                            int tagstart = line.IndexOf(".Tag = ") + 7;
                            int tagEnd = line.IndexOf(";");
                            int nr = Convert.ToInt32(line.Substring(tagstart, tagEnd - tagstart));
                            if (TagTransfers.ContainsKey(nr))
                            {
                                builder.Add("            " + variableName + ".Tag = " + TagTransfers[nr].Tag + ";");
                            }
                        }

                    }
                    
                    else
                    {
                        builder.Add(line);
                    }



                }

                reader.Close();
                File.WriteAllLines(newPath, builder.ToArray());
                formidx++;
            }
        }

    }
}
